package gui_layer;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.InternalFrameEvent;

import model_layer.Customer;
import ctr_layer.CustomerCtr;

@SuppressWarnings("serial")
public class NewCustomerGUI extends JInternalFrame
{
	private CustomerCtr cust_ctr;
	private Customer customer;
	private boolean edit;
	private JPanel contentPane;
	private JTextField name_tf;
	private JTextField phone_nr_tf;
	private JTextField email_tf;
	private JTextField address_tf;
	private JTextField zipcode_tf;
	private JTextField city_tf;
	private JTextField discount_tf;
	private JLabel inc_name_lbl;
	private JLabel inc_phone_lbl;
	private JLabel inc_email_lbl;
	private JLabel inc_address_lbl;
	private JLabel inc_zipcode_lbl;
	private JLabel inc_city_lbl;
	private JLabel inc_discount_lbl;
	private JLabel inc_phone_email_lbl;
	private JLabel[] empty_field;
	private JButton btnCancel;
	private JLabel inc_taken_name_lbl;
	private JLabel inc_name_number_lbl;

	/**
	 * Create the frame.
	 */
	public NewCustomerGUI(Customer cust, boolean edit)
	{
		setIconifiable(true);
		setClosable(true);
		customer = cust;
		this.edit = edit;
		cust_ctr = new CustomerCtr();
		setResizable(false);
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		try
		{
			setFrameIcon(new ImageIcon(NewCustomerGUI.class.getResource("/img/icon.png")));
		}
		catch (Exception e)
		{	
		}
		//setBounds(100, 100, 472, 300);	//incorrect size
		setBounds(100, 100, 289, 300); //correct size
		contentPane = new JPanel();
		contentPane.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		make_lbls();
		make_tfs();
		
		if(customer == null)
		{
			setTitle("Add Customer");
			make_incorrect_lbls();
			make_new_btns();
			add_listeners();
			make_security_blockades();
		}
		else
		{
			if(edit)
			{
				setTitle("Edit Customer");
				make_edit_btns();
				make_incorrect_lbls();
				add_listeners();
			}
			else
			{
				setTitle("Customer Details");
				make_uneditable();
				make_details_btn();
			}
			
			fill_fields();
		}
		

	}
	
	private void change_to_display_correct()
	{
		setSize(289, 300);
		btnCancel.setBounds(171, 227, 89, 23);
	}
	
	private void change_to_display_incorrect()
	{
		setSize(472, 300);
		btnCancel.setBounds(311, 227, 89, 23);
	}
	
	private void make_new_btns()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JButton btnCreate = new JButton("Create");
		btnCreate.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnCreate.setForeground(Color.WHITE);
		btnCreate.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnCreate.setContentAreaFilled(false);
		btnCreate.setOpaque(true);
		btnCreate.setBorder(new RoundedCornerBorder());
		btnCreate.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				create_customer();
				
			}
		});
		btnCreate.setBounds(10, 227, 89, 23); 
		contentPane.add(btnCreate);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnCancel.setForeground(Color.WHITE);
		btnCancel.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnCancel.setContentAreaFilled(false);
		btnCancel.setOpaque(true);
		btnCancel.setBorder(new RoundedCornerBorder());
		btnCancel.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				dispose();
				fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
				
			}
		});
		btnCancel.setBounds(171, 227, 89, 23);
		contentPane.add(btnCancel);
	}
	
	private void make_edit_btns()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JButton btnCreate = new JButton("Save");
		btnCreate.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnCreate.setForeground(Color.WHITE);
		btnCreate.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnCreate.setContentAreaFilled(false);
		btnCreate.setOpaque(true);
		btnCreate.setBorder(new RoundedCornerBorder());
		btnCreate.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				edit_customer();
				
			}
		});
		btnCreate.setBounds(10, 227, 89, 23); 
		contentPane.add(btnCreate);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnCancel.setForeground(Color.WHITE);
		btnCancel.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnCancel.setContentAreaFilled(false);
		btnCancel.setOpaque(true);
		btnCancel.setBorder(new RoundedCornerBorder());
		btnCancel.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				dispose();
				fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
				
			}
		});
		btnCancel.setBounds(171, 227, 89, 23);
		contentPane.add(btnCancel);
	}
	
	private void make_details_btn()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JButton btnOk = new JButton("OK");
		btnOk.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnOk.setForeground(Color.WHITE);
		btnOk.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnOk.setContentAreaFilled(false);
		btnOk.setOpaque(true);
		btnOk.setBorder(new RoundedCornerBorder());
		btnOk.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				dispose();
				fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
				
			}
		});
		btnOk.setBounds(93, 225, 89, 23);
		contentPane.add(btnOk);
	}
	
	private void make_lbls()
	{
		JLabel lblName = new JLabel("Name");
		lblName.setBounds(10, 11, 46, 14);
		lblName.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblName.setForeground(Color.WHITE);
		contentPane.add(lblName);
		
		JLabel lblPhoneNumber = new JLabel("Phone number");
		lblPhoneNumber.setBounds(10, 36, 89, 14);
		lblPhoneNumber.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblPhoneNumber.setForeground(Color.WHITE);
		contentPane.add(lblPhoneNumber);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(10, 61, 46, 14);
		lblEmail.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblEmail.setForeground(Color.WHITE);
		contentPane.add(lblEmail);
		
		JLabel lblAddress = new JLabel("Address");
		lblAddress.setBounds(10, 86, 68, 14);
		lblAddress.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblAddress.setForeground(Color.WHITE);
		contentPane.add(lblAddress);
		
		JLabel lblZipcode = new JLabel("Zipcode");
		lblZipcode.setBounds(10, 111, 46, 14);
		lblZipcode.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblZipcode.setForeground(Color.WHITE);
		contentPane.add(lblZipcode);
		
		JLabel lblCity = new JLabel("City");
		lblCity.setBounds(10, 136, 46, 14);
		lblCity.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblCity.setForeground(Color.WHITE);
		contentPane.add(lblCity);
		
		JLabel lblDiscount = new JLabel("Discount");
		lblDiscount.setBounds(10, 161, 68, 14);
		lblDiscount.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblDiscount.setForeground(Color.WHITE);
		contentPane.add(lblDiscount);

		JLabel label = new JLabel("%");
		label.setBounds(164, 161, 17, 14);
		label.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		label.setForeground(Color.WHITE);
		contentPane.add(label);
	}
	
	private void make_tfs()
	{
		name_tf = new JTextField();
		name_tf.setBounds(108, 8, 152, 20);
		name_tf.setBorder(new RoundedCornerBorder());
		name_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(name_tf);
		name_tf.setColumns(10);
		
		phone_nr_tf = new JTextField();
		phone_nr_tf.setBounds(109, 33, 151, 20);
		phone_nr_tf.setBorder(new RoundedCornerBorder());
		phone_nr_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(phone_nr_tf);
		phone_nr_tf.setColumns(10);
		
		email_tf = new JTextField();
		email_tf.setBounds(108, 58, 152, 20);
		email_tf.setBorder(new RoundedCornerBorder());
		email_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(email_tf);
		email_tf.setColumns(10);
		
		address_tf = new JTextField();
		address_tf.setBounds(108, 83, 152, 20);
		address_tf.setBorder(new RoundedCornerBorder());
		address_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(address_tf);
		address_tf.setColumns(10);
		
		zipcode_tf = new JTextField();
		zipcode_tf.setBounds(108, 108, 152, 20);
		zipcode_tf.setBorder(new RoundedCornerBorder());
		zipcode_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(zipcode_tf);
		zipcode_tf.setColumns(10);
		
		city_tf = new JTextField();
		city_tf.setBounds(108, 133, 152, 20);
		city_tf.setBorder(new RoundedCornerBorder());
		city_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(city_tf);
		city_tf.setColumns(10);
		
		discount_tf = new JTextField();
		discount_tf.setBounds(108, 158, 46, 20);
		discount_tf.setBorder(new RoundedCornerBorder());
		discount_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		contentPane.add(discount_tf);
		discount_tf.setColumns(10);
		
		
	}
	
	private void add_listeners()
	{
		name_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					phone_nr_tf.requestFocus();
				}
			}
		});
		phone_nr_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					email_tf.requestFocus();
				}
			}
		});
		email_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					address_tf.requestFocus();
				}
			}
		});
		address_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					zipcode_tf.requestFocus();
				}
			}
		});
		zipcode_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					city_tf.requestFocus();
				}
			}
		});
		zipcode_tf.addFocusListener(new FocusAdapter()
		{
			@Override
			public void focusLost(FocusEvent e)
			{
				String zipcode = convert_zipcode(zipcode_tf.getText());
				if(zipcode != null)
				{
					if(customer == null || !customer.getZipcode().equals(zipcode))
					{
						String city = cust_ctr.find_city_by_zipcode(zipcode);
						if(city != null)
						{
							city_tf.setText(city);
						}
					}
				}
			}
		});
		if(CurrentAccount.get_privilage_level() != 0)
		{
			city_tf.addKeyListener(new KeyAdapter()
			{
				@Override
				public void keyPressed(KeyEvent e)
				{
					if(e.getKeyCode() == KeyEvent.VK_ENTER)
					{
						discount_tf.requestFocus();
					}
				}
			});
			
			discount_tf.addKeyListener(new KeyAdapter()
			{
				@Override
				public void keyPressed(KeyEvent e)
				{
					if(e.getKeyCode() == KeyEvent.VK_ENTER)
					{
						if(edit)
						{
							edit_customer();
						}
						else
						{
							create_customer();
						}
					}
				}
			});
		}
		else
		{
			city_tf.addKeyListener(new KeyAdapter()
			{
				@Override
				public void keyPressed(KeyEvent e)
				{
					if(e.getKeyCode() == KeyEvent.VK_ENTER)
					{
						if(edit)
						{
							edit_customer();
						}
						else
						{
							create_customer();
						}
					}
				}
			});
		}
		
	}
	
	private void make_incorrect_lbls()
	{
		inc_name_lbl = new JLabel("Must be max 64 characters.");
		inc_name_lbl.setForeground(Color.RED);
		inc_name_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_name_lbl.setBounds(270, 11, 161, 14);
		inc_name_lbl.setVisible(false);
		contentPane.add(inc_name_lbl);
		
		inc_phone_lbl = new JLabel("Incorrect phone number.");
		inc_phone_lbl.setForeground(Color.RED);
		inc_phone_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_phone_lbl.setBounds(270, 36, 161, 14);
		inc_phone_lbl.setVisible(false);
		contentPane.add(inc_phone_lbl);
		
		inc_email_lbl = new JLabel("Incorrect email.");
		inc_email_lbl.setForeground(Color.RED);
		inc_email_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_email_lbl.setBounds(270, 61, 119, 14);
		inc_email_lbl.setVisible(false);
		contentPane.add(inc_email_lbl);
		
		inc_address_lbl = new JLabel("Must be max 64 characters.");
		inc_address_lbl.setForeground(Color.RED);
		inc_address_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_address_lbl.setBounds(270, 86, 155, 14);
		inc_address_lbl.setVisible(false);
		contentPane.add(inc_address_lbl);
		
		inc_zipcode_lbl = new JLabel("Incorrect zipcode.");
		inc_zipcode_lbl.setForeground(Color.RED);
		inc_zipcode_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_zipcode_lbl.setBounds(270, 111, 131, 14);
		inc_zipcode_lbl.setVisible(false);
		contentPane.add(inc_zipcode_lbl);
		
		inc_city_lbl = new JLabel("Must be max 32 characters.");
		inc_city_lbl.setForeground(Color.RED);
		inc_city_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_city_lbl.setBounds(271, 136, 154, 14);
		inc_city_lbl.setVisible(false);
		contentPane.add(inc_city_lbl);
		
		inc_discount_lbl = new JLabel("Discount must be a number between 0 and 100.");
		inc_discount_lbl.setForeground(Color.RED);
		inc_discount_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_discount_lbl.setBounds(187, 161, 276, 14);
		inc_discount_lbl.setVisible(false);
		contentPane.add(inc_discount_lbl);
		
		inc_phone_email_lbl = new JLabel("<html>You must enter phone number <br>or email or both.</html>");
		inc_phone_email_lbl.setForeground(Color.RED);
		inc_phone_email_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_phone_email_lbl.setVisible(false);
		inc_phone_email_lbl.setBounds(270, 33, 193, 36);
		contentPane.add(inc_phone_email_lbl);
		
		inc_taken_name_lbl = new JLabel("Name is already taken.");
		inc_taken_name_lbl.setForeground(Color.RED);
		inc_taken_name_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_taken_name_lbl.setBounds(270, 11, 136, 14);
		inc_taken_name_lbl.setVisible(false);
		contentPane.add(inc_taken_name_lbl);
		
		inc_name_number_lbl = new JLabel("Cannot be a number.");
		inc_name_number_lbl.setForeground(Color.RED);
		inc_name_number_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_name_number_lbl.setBounds(270, 11, 131, 14);
		inc_name_number_lbl.setVisible(false);
		contentPane.add(inc_name_number_lbl);
		
		int[] y_positions = { 11, 136 };
		empty_field = new JLabel[3];
		for(int i = 0; i < y_positions.length; i++)
		{
			JLabel temp = new JLabel("Field cannot be empty.");
			temp.setForeground(Color.RED);
			temp.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
			temp.setBounds(270, y_positions[i], 150, 14);
			temp.setVisible(false);
			contentPane.add(temp);
			empty_field[i] = temp;
		}
		
	}
	
	private void make_uneditable()
	{
		name_tf.setEditable(false);
		phone_nr_tf.setEditable(false);
		email_tf.setEditable(false);
		address_tf.setEditable(false);
		zipcode_tf.setEditable(false);
		city_tf.setEditable(false);
		discount_tf.setEditable(false);
	}
	
	private void make_security_blockades()
	{
		if(CurrentAccount.get_privilage_level() == 0)
		{
			discount_tf.setText("0");
			discount_tf.setEnabled(false);
		}
	}
	
	private void fill_fields()
	{
		name_tf.setText(customer.getName());
		phone_nr_tf.setText(customer.getPhone_nr());
		email_tf.setText(customer.getEmail());
		address_tf.setText(customer.getAddress());
		zipcode_tf.setText(customer.getZipcode());
		city_tf.setText(customer.getCity());
		discount_tf.setText(String.valueOf(customer.getDiscount()));
	}
	
	private void create_customer()
	{
		if(check_for_correctness())
		{
			String name = name_tf.getText();
			String phone_number = phone_nr_tf.getText();
			String email = email_tf.getText();
			String address = address_tf.getText();
			String city = city_tf.getText();
			String zipcode = zipcode_tf.getText();
			String discount = discount_tf.getText();
			if(discount.length() == 0)
			{
				discount = "0";
			}
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to save this customer?", "Save", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(cust_ctr.insert_customer(name, phone_number, email, address, convert_zipcode(zipcode), city, Float.parseFloat(discount)))
				{
					JOptionPane.showMessageDialog(this, "Customer was successfully created!", "Success", JOptionPane.PLAIN_MESSAGE);
					dispose();
					fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
				}
				else
				{
					JOptionPane.showMessageDialog(this, "Error occured while creating a customer!\nCustomer was not inserted.", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}
	
	private void edit_customer()
	{
		if(check_for_correctness())
		{
			String name = name_tf.getText();
			String phone_number = phone_nr_tf.getText();
			String email = email_tf.getText();
			String address = address_tf.getText();
			String city = city_tf.getText();
			String zipcode = zipcode_tf.getText();
			String discount = discount_tf.getText();
			if(discount.length() == 0)
			{
				discount = "0";
			}
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to update this customer?", "Save", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				customer.setAddress(address);
				customer.setCity(city);
				customer.setDiscount(Float.parseFloat(discount));
				customer.setEmail(email);
				customer.setName(name);
				customer.setPhone_nr(phone_number);
				customer.setZipcode(convert_zipcode(zipcode));
				if(cust_ctr.update_customer(customer))
				{
					JOptionPane.showMessageDialog(this, "Customer was successfully updated!", "Success", JOptionPane.PLAIN_MESSAGE);
					dispose();
					fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
				}
				else
				{
					JOptionPane.showMessageDialog(this, "Error occured while saving a customer!\nCustomer was not updated.", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}
	
	private boolean check_for_correctness()
	{
		boolean correct = true;
		String name = name_tf.getText();
		String phone_number = phone_nr_tf.getText();
		String email = email_tf.getText();
		String address = address_tf.getText();
		String city = city_tf.getText();
		String zipcode = zipcode_tf.getText();
		String discount = discount_tf.getText();
		
		
		if(name.length() == 0)
		{
			empty_field[0].setVisible(true);
			inc_name_lbl.setVisible(false);
			inc_taken_name_lbl.setVisible(false);
			correct = false;
		}
		else if(name.length() > 32)
		{
			inc_name_number_lbl.setVisible(false);
			inc_name_lbl.setVisible(true);
			empty_field[0].setVisible(false);
			inc_taken_name_lbl.setVisible(false);
			correct = false;
		}
		else if(is_int(name))
		{
			inc_name_number_lbl.setVisible(true);
			inc_name_lbl.setVisible(false);
			empty_field[0].setVisible(false);
			inc_taken_name_lbl.setVisible(false);
			correct = false;
		}
		else if((customer == null && cust_ctr.name_exists(name)) || (customer!= null && !customer.getName().equals(name) && cust_ctr.name_exists(name)))
		{
			inc_name_number_lbl.setVisible(false);
			inc_name_lbl.setVisible(false);
			empty_field[0].setVisible(false);
			inc_taken_name_lbl.setVisible(true);
			correct = false;
		}
		else
		{
			inc_name_number_lbl.setVisible(false);
			inc_name_lbl.setVisible(false);
			empty_field[0].setVisible(false);
			inc_taken_name_lbl.setVisible(false);
		}
		
		
		if(address.length() > 64)
		{
			inc_address_lbl.setVisible(true);
			correct = false;
		}
		else inc_address_lbl.setVisible(false);
		
		if(phone_number.length() != 0 && !phone_number.matches("(?:\\+[0-9]{2,2}\\-?)?[0-9](?:\\-?[0-9]){7,10}"))
		{
			inc_phone_lbl.setVisible(true);
			correct = false;
		}
		else inc_phone_lbl.setVisible(false);
		
		if(email.length() == 0 && phone_number.length() == 0)
		{
			inc_phone_email_lbl.setVisible(true);
			inc_email_lbl.setVisible(false);
			correct = false;
		}
		else if(email.length() != 0 && !email.matches("^[\\w\\._%\\+\\-]{1,64}@[\\w\\.\\-]{1,188}\\.[A-Za-z]{2,4}$"))
		{
			inc_phone_email_lbl.setVisible(false);
			inc_email_lbl.setVisible(true);
			correct = false;
		}
		else 
		{
			inc_phone_email_lbl.setVisible(false);
			inc_email_lbl.setVisible(false);
		}
		
		if(convert_zipcode(zipcode) == null)
		{
			inc_zipcode_lbl.setVisible(true);
			correct = false;
		}
		
		if(city.length() >32)
		{
			inc_city_lbl.setVisible(true);
			correct = false;
		}
		else inc_city_lbl.setVisible(false);
		
		if(city.length() == 0)
		{
			empty_field[1].setVisible(true);
			correct = false;
		}
		else empty_field[1].setVisible(false);
		
		if((discount.length() != 0) && !is_correct_discount(discount))
		{
			inc_discount_lbl.setVisible(true);
			correct = false;
		}
		else inc_discount_lbl.setVisible(false);
		
		if(!correct)
		{
			change_to_display_incorrect();
		}
		else
		{
			change_to_display_correct();
		}
		
		return correct;
	}
	
	private String convert_zipcode(String zipcode)
	{
		if(zipcode.matches("^[0-9]{4,4}$"))
		{
			return "BG" + zipcode;
		}
		else if(zipcode.matches("^[A-Z]{2,3}(?:\\-?[0-9]){4,7}"))
		{
			return zipcode.replace("-", "");
		}
		else return null;
	}
	
	private boolean is_correct_discount(String disc)
	{
		try
		{
			float f = Float.parseFloat(disc);
			return f < 100 && f >= 0;
		}
		catch(NumberFormatException e)
		{
			return false;
		}
	}
	
	private boolean is_int(String input)
	{
		try
		{
			Integer.parseInt(input);
			return true;
		}
		catch(NumberFormatException e)
		{
			return false;
		}
	}
}
